"""
Case Type   : HugePage
Case Name   : huge_page_size参数多种方式设置及异常设置验证
Create At   : 2024/11/08
Owner       : liu-tong-8848
Description :
              1、主机enable_huge_pages设置为on，查询系统支持的大页大小
              2、设置huge_page_size为非步骤1查询的结果，重启数据库
              3、设置huge_page_size为包含字符
              4、设置huge_page_size单位不正确
              5、不带单位设置huge_page_size范围不正确
              6、带单位设置huge_page_size范围不正确
              7、设置huge_page_size为小数
              8、设置huge_page_size为0，重启数据库
              9、alter system方式正常设置，重启数据库，查询生效情况
              10、alter system方式设置非系统支持的大页，重启数据库
              11、alter system方式异常设置
              12、清理环境
Expect      :
              1、成功
              2、重启数据库失败，合理报错
              3、失败，合理报错
              4、失败，合理报错
              5、失败，合理报错
              6、失败，合理报错
              7、失败，合理报错
              8、重启数据库成功
              9、成功
              10、重启数据库失败，合理报错
              11、设置参数失败，合理报错
              12、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.com = Common('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')
        self.db_root_node = Node(node='PrimaryRoot')

    def test_startdb(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

        step1 = '--step1:主机enable_huge_pages设置为on，查看系统支持的大页大小;expect:成功'
        self.log.info(step1)
        gs_guc_set_cmd = "enable_huge_pages=on"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step1)
        gs_guc_set_cmd = "shared_buffers = 128MB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.log.info(gs_guc_set_result)
        self.assertTrue(gs_guc_set_result, "执行失败" + step1)

        change_hugepages_cmd = "echo 1000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step1)

        select_groupid_cmd = "id -g"
        groupid = self.db_user_node.sh(select_groupid_cmd).result()
        self.log.info("groupid: " + groupid)
        self.assertNotIn('error', groupid, "执行失败" + step1)

        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=" + groupid
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step1)

        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step1)
        enable_huge_pages = self.com.show_param("enable_huge_pages")
        self.assertTrue("on" in enable_huge_pages, "执行失败" + step1)
        cat_hugepagesize_cmd = "ls /sys/kernel/mm/hugepages"
        run_change_hugepages = self.db_root_node.sh(cat_hugepagesize_cmd)
        change_hugepages_result = run_change_hugepages.result()
        self.log.info(change_hugepages_result)
        self.assertTrue(run_change_hugepages, "执行失败" + step1)

        step2 = '--step2:设置huge_page_size为非步骤1查询的结果，重启数据库;expect:重启数据库失败，合理报错'
        self.log.info(step2)
        gs_guc_set_cmd = "huge_page_size=222MB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step2)

        step3 = '--step3:设置huge_page_size为包含字符;expect:失败，合理报错'
        self.log.info(step3)
        gs_guc_set_cmd = "huge_page_size=222aMB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step3)

        step4 = '--step4:设置huge_page_size单位不正确;expect:失败，合理报错'
        self.log.info(step4)
        gs_guc_set_cmd = "huge_page_size=2048TB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step4)
        gs_guc_set_cmd = "huge_page_size=2048mB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step4)
        gs_guc_set_cmd = "huge_page_size=2048KB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step4)

        step5 = '--step5:不带单位设置huge_page_size范围不正确;expect:失败，合理报错'
        self.log.info(step5)
        gs_guc_set_cmd = "huge_page_size=1073741824"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step5)
        gs_guc_set_cmd = "huge_page_size=-1"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step5)

        step6 = '--step6:带单位设置huge_page_size范围不正确;expect:失败，合理报错'
        self.log.info(step6)
        gs_guc_set_cmd = "huge_page_size=8193GB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step6)

        step7 = '--step7:带单位设置huge_page_size范围不正确;expect:失败，合理报错'
        self.log.info(step7)
        gs_guc_set_cmd = "huge_page_size=22.02"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step7)
        gs_guc_set_cmd = "huge_page_size=22.02MB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertFalse(gs_guc_set_result, "执行失败" + step7)

        step8 = '--step8:设置huge_page_size为0，重启数据库;expect:成功'
        self.log.info(step8)
        gs_guc_set_cmd = "huge_page_size=0"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step8)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step8)

        step9 = '--step9:alter system方式正常设置，重启数据库，查询生效情况;expect:成功'
        self.log.info(step9)
        max_hugepage_size = change_hugepages_result.split('\n')[0].split('-')[1]
        self.log.info(max_hugepage_size)
        set_max_hugepage_size_cmd = "alter system set huge_page_size to '1GB';"
        result = self.primary_sh.execut_db_sql(set_max_hugepage_size_cmd)
        self.assertIn(self.constant.SET_SUCCESS_MSG, result, "执行失败" + step9)
        change_hugepages_cmd = "echo 2 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step9)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step9)

        step10 = '--step10:alter system方式设置非系统支持的大页，重启数据库，查询生效情况;expect:重启数据库失败，合理报错'
        self.log.info(step10)
        gs_guc_set_cmd = "alter system set huge_page_size to '222MB';"
        result = self.primary_sh.execut_db_sql(gs_guc_set_cmd)
        self.assertIn(self.constant.SET_SUCCESS_MSG, result, "执行失败" + step10)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step10)

        step11 = '--step11:alter system方式异常设置;expect:设置参数失败，合理报错'
        self.log.info(step11)
        set_max_hugepage_size_cmd = "huge_page_size=" + max_hugepage_size
        self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, set_max_hugepage_size_cmd, single=True)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step11)
        gs_guc_set_cmd = "alter system set huge_page_size to '112a';"
        result = self.primary_sh.execut_db_sql(gs_guc_set_cmd)
        self.assertIn("ERROR", result, "执行失败" + step11)
        gs_guc_set_cmd = "alter system set huge_page_size to '2048TB';"
        result = self.primary_sh.execut_db_sql(gs_guc_set_cmd)
        self.assertIn("ERROR", result, "执行失败" + step11)
        gs_guc_set_cmd = "alter system set huge_page_size to '22.02';"
        result = self.primary_sh.execut_db_sql(gs_guc_set_cmd)
        self.assertIn("ERROR", result, "执行失败" + step11)
        gs_guc_set_cmd = "alter system set huge_page_size to '1073741824';"
        result = self.primary_sh.execut_db_sql(gs_guc_set_cmd)
        self.assertIn("ERROR", result, "执行失败" + step11)
        gs_guc_set_cmd = "alter system set huge_page_size to '-1';"
        result = self.primary_sh.execut_db_sql(gs_guc_set_cmd)
        self.assertIn("ERROR", result, "执行失败" + step11)

    def tearDown(self):
        step12 = '--step12:清理环境;expect:清理环境成功'
        self.log.info(step12)
        gs_guc_set_cmd = "huge_page_size=2048kB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step12)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step12)
        change_hugepages_cmd = "echo 0 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step12)
        use_sysctl_cmd = "sysctl -p"
        self.db_root_node.sh(use_sysctl_cmd)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
